var add_submit = function(){

	// 再根据条件显示
	var div;
	var sel = DATA_PROD_TYPE[$("#PROD_TYPE").val()];
	switch(sel){
		case "IDC":
			div = "#page_idc";
			break;
		case "专线":
			div = "#page_line";
			break;
		case "集团电话":
			div = "#page_group";
			break;
		case "物联网":
			div = "#page_internet";
			break;
		default:
			break;
	}
	
	var passUrlObj = {
		"PROD_TYPE":$("#PROD_TYPE").val(),
		"OPERATOR":$("#OPERATOR").val(),
		"group_name":$("#group_name").val(),
		"groupNo":$("#group_no").val(),
		"k1":$(div + " #k1").val(),
		"k2":$(div + " #k2").val(),
		"k3":$(div + " #k3").val(),
		"k4":$(div + " #k4").val(),
		"k5":$(div + " #k5").val(),
		"k6":$(div + " #k6").val(),
		"k7":$(div + " #k7").val(),
		"k8":$(div + " #k8").val()
	};
	var CUR_PROD_TYPE = DATA_PROD_TYPE_GROUP[passUrlObj.PROD_TYPE]
	// var params = passUrlObj;
	var params = {
		"requestData":
			"<OPER_TYPE>ADD</OPER_TYPE>"+
			"<OPER_TIME>"+dateFormat(new Date(), "YYYY/MM/DD hh:mm:ss")+"</OPER_TIME>"+
			"<CUST_CODE>"+passUrlObj.groupNo+"</CUST_CODE>"+
			"<OPERATOR>"+passUrlObj.OPERATOR+"</OPERATOR>"+
			"<PROD_TYPE>"+passUrlObj.PROD_TYPE+"</PROD_TYPE>"+
			"<PROD_ID></PROD_ID>"+
			"<PROD_INFO><"+CUR_PROD_TYPE[0]+">"+
			"<"+CUR_PROD_TYPE[1]+">"+passUrlObj.k1+"</"+CUR_PROD_TYPE[1]+">"+
			"<"+CUR_PROD_TYPE[2]+">"+passUrlObj.k2+"</"+CUR_PROD_TYPE[2]+">"+
			"<"+CUR_PROD_TYPE[3]+">"+passUrlObj.k3+"</"+CUR_PROD_TYPE[3]+">"+
			"<"+CUR_PROD_TYPE[4]+">"+passUrlObj.k4+"</"+CUR_PROD_TYPE[4]+">"+
			"<"+CUR_PROD_TYPE[5]+">"+passUrlObj.k5+"</"+CUR_PROD_TYPE[5]+">"+
			"<"+CUR_PROD_TYPE[6]+">"+passUrlObj.k6+"</"+CUR_PROD_TYPE[6]+">"+
			"<"+CUR_PROD_TYPE[7]+">"+passUrlObj.k7+"</"+CUR_PROD_TYPE[7]+">"+
			"<"+CUR_PROD_TYPE[8]+">"+passUrlObj.k8+"</"+CUR_PROD_TYPE[8]+">"+
			"</"+CUR_PROD_TYPE[0]+"></PROD_INFO>"
	};

	$.ajax({
		type: "post",
      	contentType: "application/json",
		data: JSON.stringify(params),
		// url: "../../../opponent/addIdc",
		url: Constants.restHost + "rest/opp/compProdChg",
		dataType: "json",
		async: true,
		headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
			console.log(data);
			if(data.rtnCode!=1000){
				showErrorMsg(data.rtnMsg);
				console.log("提交失败！code:"+data.rtnMsg);
				return;
			}
			if(data.return.HEADER.RESPONSE.CODE != 0000) {
				showErrorMsg(data.return.HEADER.RESPONSE.DESC);
				console.log("提交失败！code:"+data.return.HEADER.RESPONSE.CODE);
				return;
			}
			console.log("消息:"+data.return.RESULT.RETURN_MSG);
			
			$("#submit_success").click();
		},
		error : function(textStatus) {
			showErrorMsg("提交数据失败！服务器没有返回。");
			disabled_submit_button(false);
			console.log("提交数据失败！服务器没有返回。");
		}
	});
}
var showErrorMsg = function(msg){
	$("#err_msg_info").html(msg);
	$("#submit_error").click();
}

var hideDiv = function(){
	// 全部隐藏
	$("#page_idc").hide();
	$("#page_line").hide();
	$("#page_group").hide();
	$("#page_internet").hide();
}

$(function(){
	DATA_OPERATORS = ["", "联通", "电信"],
    DATA_PROD_TYPE = ["", "IDC", "专线", "集团电话", "物联网"],
    DATA_PROD_TYPE_GROUP = [
    	[],
    	["IDC_INFO","INT_BANDWIDTH","MACHINE_ROOM","RACK_NUM","RACK_POWER","COST","START_DATE","END_DATE","AMOUNT"],
    	["LINE_INFO","BUSI_TYPE","BANDWIDTH","ARTI_NUM","COST","START_DATE","END_DATE","AMOUNT","AVE_TRAFFIC"],
    	["TEL_INFO","ACCESS","INSTALLED_NUM","INSTALLED_ADDR","SEG_NO","COST","START_DATE","END_DATE","AMOUNT"],
    	["IOT_INFO","NET_TYPE","BUSI_TYPE","APP_SCENE","PACK_COST","COST","START_DATE","END_DATE","AMOUNT"]
    ];
	hideDiv();
	$("#page_idc").show();
	
	$("#group_name").val(decodeURIComponent(getParamValue("group_name")));
	$("#group_no").val(getParamValue("group_no"));
	
	$('.date-input').on({
		'keyup': function(e) {
			var dateInput = $(this).val();
			if(e.keyCode >= 48 && e.keyCode <= 57 || e.keyCode >= 96 && e.keyCode <= 105) {
				if(dateInput.length === 4) {
					$(this).val(dateInput + '-');
				}
				if(dateInput.length === 7) {
					$(this).val(dateInput + '-');
				}
			} else if(e.keyCode === 8) {
				if(dateInput.length === 8) {
					$(this).val(dateInput.replace(/[-]$/, ""));
				}
				if(dateInput.length === 5) {
					$(this).val(dateInput.replace(/[-]$/, ""));
				}
			}
		}
	});
	// 显示 隐藏 新增界面
	$("#PROD_TYPE").on('change', function() {
		// 先全部隐藏
		hideDiv();
		
		// 再根据条件显示
		var sel = DATA_PROD_TYPE[$("#PROD_TYPE").val()];
		switch(sel){
			case "IDC":
				$("#page_idc").show();
				break;
			case "专线":
				$("#page_line").show();
				break;
			case "集团电话":
				$("#page_group").show();
				break;
			case "物联网":
				$("#page_internet").show();
				break;
			default:
				break;
		}
	});
});



var back_list = function(){
	window.location.href = "opponent_list.html" + "?" + getParams();
}

var disabled_submit_button = function(open){
		if(open){
			$("#submit_wite").click();
			$("#add_submit").html(" <img width='20px;' src='../../../img/progress.gif'> 提交中...");
			$("#add_submit").attr("disabled","disabled");
		}else{
			$("#wite_close").click();
			$("#add_submit").html("提交");
			$("#add_submit").removeAttr("disabled");
		}
};

